<template>
  <div ref="goods">
    <div class="goodsearch_container">
      <div class="box" v-for="(item, index) in goodsList" :key="item.Id">
        <div class="item" @click="goDetail(item.Id)">
          <el-image :src="item.imageUrl" lazy>
            <div slot="placeholder" class="image-slot">
              加载中<span class="dot">...</span>
            </div>
          </el-image>
          <div>
            <div class="title">{{ item.title }}</div>
            <div class="footer">
              <div class="price">￥{{ item.priceStr }}</div>
              <div class="together">
                <div class="jx">精选</div>
                <div class="pin">一起拼</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[20, 30, 40, 50]"
      :page-size="7"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      background
    >
    </el-pagination>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "GoodList",
  props: [],
  data() {
    return {
      page: 1,
      pagesize: 20,
      top: 0,
    };
  },
  computed: {
    ...mapState(["searchList"]),
    word() {
      return this.$route.query.word || "";
    },
    goodsList() {
      return this.searchList.filter(
        (item, index) =>
          index <= this.page * this.pagesize - 1 &&
          index >= (this.page - 1) * this.pagesize
      );
    },
    total() {
      return this.searchList.length;
    },
  },
  created() {
    this.getGoodSearchByWord();
  },
  mounted() {
    this.top = this.$refs.goods.offsetTop - 100;
  },
  watch: {
    word() {
      this.getGoodSearchByWord();
    },
  },
  methods: {
    //根据word检索商品列表
    async getGoodSearchByWord() {
      if (this.word == "") return this.$router.push({ name: "goodhome" });
      const res = await this.$store.dispatch("getGoodSearchByWord", this.word);
      if (res === 0)
        return this.$message.info(this.word + "关键字没有商品数据");
    },
    //pagesize改变
    handleSizeChange(pagesize) {
      this.pagesize = pagesize;
    },
    //页码改变
    handleCurrentChange(page) {
      scrollTo({
        top: this.top,
      });
      this.page = page;
    },
    //点击商品进入detail页面
    goDetail(id) {
      this.$router.push({ name: "detail", params: { id } });
      scrollTo({
        top: 0,
      });
    },
  },
};
</script>

<style lang="less" scoped>
.goodsearch_container {
  display: flex;
  flex-wrap: wrap;
  .box {
    width: 20%;
    padding: 0 10px;
    margin: 0 0 10px 0;

    .item {
      border: 1px solid #000;
      height: 232px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      img {
        width: 100%;
      }
      .title {
        width: 100%;
        font-size: 20px;
        font-weight: 700;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      .footer {
        display: flex;
        justify-content: space-between;
        .price {
          font-size: 15px;
          color: red;
          font-weight: 900;
        }
        .together {
          display: flex;
          align-items: center;
          .jx {
            font-size: 12px;
            color: #fff;
            background-color: rgb(218, 75, 218);
          }
          .pin {
            font-size: 12px;
            color: red;
          }
        }
      }
    }
  }
}
</style>